//
//  MemberModel.m
//  小说
//  
//  Created by WZH on 2018/5/26.
//  Copyright © 2018年 WZH. All rights reserved.
//

#import "MemberModel.h"

@implementation MemberModel

- (void)archive{
    [NSKeyedArchiver archiveRootObject:self toFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/menber.plist"]];
}

+ (instancetype)unarchive{
    return [NSKeyedUnarchiver unarchiveObjectWithFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/menber.plist"]];
}

+ (void)addSmallModel:(SmallModel *)model1{
    MemberModel *model = [self unarchive];
    if (!model) {
        model = [[self alloc] init];
    }
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:model.infoArray];
    int flag = 1;
    for (SmallModel *ss in array) {
        if ([ss.id isEqualToString:model1.id]) {
            flag = 2;
            break;
        }
    }
    if (flag == 1) {
        [array addObject:model1];
        model.infoArray = array.copy;
        [model archive];
    }
}

+ (void)addTitle:(NSString *)title url:(NSString *)url id:(NSString *)id{
    MemberModel *model = [self unarchive];
    if (!model) {
        model = [[self alloc] init];
    }
    
    SmallModel *smallModle = [[SmallModel alloc] init];
    smallModle.title = title;
    smallModle.url = url;
    smallModle.id = id;
    NSMutableArray *array = [NSMutableArray arrayWithArray:model.infoArray];
    int flag = 1;
    for (SmallModel *ss in array) {
        if ([ss.id isEqualToString:id]) {
            flag = 2;
            break;
        }
    }
    if (flag == 1) {
        [array addObject:smallModle];
        model.infoArray = array.copy;
        [model archive];
    }
}


@end


@implementation SmallModel

@end
